package com.deliinsns.app.entrance;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.deliinsns.app.basedata.BaseDataDao;
import com.deliinsns.app.basedata.bean.BaseData;
import com.deliinsns.app.employer.SupplyDao;
import com.deliinsns.app.employer.bean.Supply;
import com.deliinsns.sys.BasicController;
@Controller
public class PortalEntrance extends BasicController {
	 @Autowired  
     private  SupplyDao supplyDao;  
	 @Autowired  
     private  BaseDataDao baseDataDao;  
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	 public String index(HttpServletRequest request,HttpServletResponse response)throws Exception {
		return "index";
	 }
	@RequestMapping(value = "/view/supplyInfo/{supplyId}", method = RequestMethod.GET)
	 public String supplyInfo(@PathVariable String supplyId,HttpServletRequest request,HttpServletResponse response)throws Exception {
		
		return "supplyInfo";
	}
	@RequestMapping(value = "/view/supplySearchList", method = RequestMethod.GET)
	 public String supplySearchList(HttpServletRequest request,HttpServletResponse response)throws Exception {
		String classify= request.getParameter("classify");
		String parentClassify= request.getParameter("parentClassify");
		String area= request.getParameter("area");
		String title= request.getParameter("title");
		Integer classifyValue=0,parentClassifyValue=0,areaValue=0;
		if(StringUtils.isNotEmpty(classify)&&StringUtils.isNumeric(classify))
			classifyValue= Integer.valueOf(classify);
		if(StringUtils.isNotEmpty(parentClassify)&&StringUtils.isNumeric(parentClassify))
			parentClassifyValue= Integer.valueOf(parentClassify);
		if(StringUtils.isNotEmpty(area)&&StringUtils.isNumeric(area))
			areaValue= Integer.valueOf(area);
		String pageStr= request.getParameter("page");//第几页
		String sizeStr= request.getParameter("size");//页的大小
		Integer page=1,size=10,total=0;
		if(StringUtils.isNotEmpty(pageStr)&&StringUtils.isNumeric(pageStr))
			 page=  Integer.parseInt(pageStr);
		page=	page<1?1:page;
		if(StringUtils.isNotEmpty(sizeStr)&&StringUtils.isNumeric(sizeStr))
			size=  Integer.parseInt(sizeStr);
		List<Supply> list= null;
		DetachedCriteria criteria = DetachedCriteria.forClass(Supply.class);
		criteria.addOrder(Order.desc("createTime"));  
		List<Integer> idList =new ArrayList<Integer>();
		if(parentClassifyValue!=0){
			 List<BaseData> classifyList=new ArrayList<BaseData>();
			 classifyList = baseDataDao.findByHql("from BaseData b where  b.dataType='jobclassify' and b.parent="+parentClassifyValue+"");
			 for(BaseData  obj : classifyList ){
				 System.out.println(obj.getId());
				 idList.add(obj.getId());
			 }
		}
		if(classifyValue!=0)
				 idList.add(classifyValue);
		if(idList.size()>0)
			criteria.add(Property.forName("classify").in(idList)) ;
	    if(areaValue!=0)
	    	criteria.add(Property.forName("workArea").eq(areaValue)) ;
	    if(StringUtils.isNotEmpty(title))
	    	criteria.add(Property.forName("title").like("%"+title+"%")) ;
		total= supplyDao.findByCriteria(criteria).size();
		list= supplyDao.findByCriteria(criteria,(page-1)*size,size);
		request.setAttribute("supplyList", list);
		request.setAttribute("size", size);//每页的大小
		request.setAttribute("page", page);//表示当前是第几页
		request.setAttribute("total", total); //表示总页数
		request.setAttribute("classify", parentClassify); 
		request.setAttribute("area", area); 
		request.setAttribute("title", title); 
		return "supplySearchList";
	 }
	@RequestMapping(value = "/view/login", method = RequestMethod.GET)
	 public String login(HttpServletRequest request,HttpServletResponse response)throws Exception {
		return "login";
	 }
	@RequestMapping(value = "/view/register", method = RequestMethod.GET)
	 public String register(HttpServletRequest request,HttpServletResponse response)throws Exception {
		String userType= request.getParameter("userType");
		request.setAttribute("userType", userType);
		return "register";
	 }
	
}
